#ifndef _MUNIX_IOPORT_H
#define	_MUNIX_IOPORT_H
/*******************************************************************************
 ** Name: ioport.h                                                         **
 ** Description:              **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the license.txt within              **
 ** this distribution or at http://www.munixos.net/licensing.                 **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $HOME/license.txt.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 **                                                                           **
 ** Copyright (c) 2009-2010  Barry Gian James <bjames@munixos.net>            **
 ** All rights reserved.                                                      **
 **                                                                           **
 ** Ref: $HeadURL$
 ******************************************************************************/
// Portions (C) 2011 OpenHouseware, LLC.
// All Rights Reserved

// $Id$
// Last Modified $DateTime$ by $Author$
#include "common.h"
#include <string.h>

//! @class IOPort
//! @brief Describes a standard GPIO port on an AVR.  Allows a common
//! point of reference for devices to refer to pins on ports of AVR's.
class IOPort
{
public:
    IOPort(volatile uint8 * p, volatile uint8* d, volatile uint8 * pins, uint8 pin, const char * n = NULL );
    virtual ~IOPort() { if (name) delete [] name; }

    inline volatile uint8 *    Port() const { return port; }
    inline volatile uint8 *    DDR() const { return ddr; }
    inline char *              Name() const { return name; }
    inline volatile uint8 *    Pins() const { return pins; }
    inline uint8               Pin() const { return pin; }
    inline bool                IsSet(uint8 pin) { return (*pins & BIT8(pin)); }

    inline void     SetPinHigh() { BSET(*port,pin); }
    inline void     SetPinLow()  { BCLR(*port,pin); }
    inline void     SetDirOut()  { BSET(*ddr,pin); }
    inline void     SetDirIn()   { BCLR(*ddr,pin); }
    inline void     SetPullup()  { BCLR(*ddr,pin); BSET(*port,pin); }

private:
    volatile uint8 *    port;   // _SFR8(0xnn)
    volatile uint8 *    ddr;    // DDRn
    volatile uint8 *    pins;   // PINn
    uint8               pin;    // PINnx
    char *              name;   // "PINB3"
};

#endif	/* _MUNIX_IOPORT_H */

